package com.dede.monochromatism.util

import com.dede.applib.util.JavaShellUtil
import java.io.DataOutputStream
import java.io.IOException

/**
 * @author hsh
 * @time 2017/6/24 024 2:33 下午.
 * @doc
 */
object ShellUtil {

    fun exeCmd(cmd: String?) {
        if (cmd == null || "" == cmd)
            return
        var process: Process? = null
        var os: DataOutputStream? = null
        try {
            process = Runtime.getRuntime().exec("su")
            os = DataOutputStream(process.outputStream)
            os.writeBytes(cmd + "\n")
            os.flush()
            os.writeBytes("exit\n")
            os.flush()
            process.waitFor()
        } catch (e: IOException) {
            e.printStackTrace()
        } catch (e: InterruptedException) {
            e.printStackTrace()
        } finally {
            try {
                os?.close()
            } catch (e: IOException) {
            }
        }
    }

    fun requestPermission(packageName: String) {
        JavaShellUtil.execCommand("pm grant $packageName android.permission.WRITE_SECURE_SETTINGS")
    }

    fun requestRoot(): Boolean {
        val result = JavaShellUtil.execCommand("")
        return result.result == 0
    }
}